home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / Two-dimens204718292007.psc / 2 d array any class final / Array2d.cls
Text File  |  2007-02-09  |  8KB  |  319 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "Array2d"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. '2-d array class based on 1-d array - more flexible than VB's 2-d
  17.  
  18. 'V2
  19. 'after a suggestion from La Volpe I've generalised this class
  20. 'to work with any type of variable
  21. 'array is stored in a variant and values are passed as variant
  22. 'the earlier version could perhaps be used as a template for
  23. 'other specific length variables like bytes or integers
  24.  
  25. 'V3 this one - probably final version
  26. '- defined user type for storing an array with bounds
  27.  
  28. 'Public type can't be put in a class module unfortunately
  29. ' and is in the form
  30.  
  31. 'Private Type TwoDArrayType
  32. 'tarray As Variant
  33. 'tRLBound As Long
  34. 'tRUBound As Long
  35. 'tCLBound As Long
  36. 'tCUBound As Long
  37. 'tHASARRAY As Boolean
  38. 'tVARTYPE As Integer
  39. 'End Type
  40.  
  41. '- the array can be redim preserved by changing lower as well as
  42. 'upper bounds
  43.  
  44. '- array type can be changed subject to conversion possibilities
  45. 'eg long to integer (provided all longs are in integer range) etc
  46. 'it'll throw up errors if you choose badly
  47.  
  48. 'Kenneth Buckmaster
  49.  
  50. Dim arr As Variant, rows As Long, cols As Long, UBarray As Long
  51. Dim RLBound As Long, RUBound As Long, CLBound As Long, CUBound As Long
  52.  
  53. 'for sort
  54. Dim arrstart As Long, colw As Long, sortvertical As Boolean, sortAscending As Boolean
  55. Dim vartypeset As Boolean
  56. Private Sub Class_Initialize()
  57. UBarray = -1
  58. End Sub
  59.  
  60. Function FetchArray(Optional RLBoundI As Long, Optional CLBoundI As Long, Optional RUBoundI As Long, Optional CUBoundI As Long, Optional Arrvartype As Integer) As Variant
  61. If IsMissing(RLBoundI) = False Then 'need all or none
  62. RLBoundI = RLBound
  63. RUBoundI = RUBound
  64. CLBoundI = CLBound
  65. CUBoundI = CUBound
  66. Arrvartype = VarType(arr(0))
  67. End If
  68. FetchArray = arr
  69. End Function
  70. Sub SetArray(ByVal RLBoundI As Long, ByVal CLBoundI As Long, ByVal RUBoundI As Long, ByVal CUBoundI As Long, newarray As Variant)
  71. vartypeset = True
  72. RLBound = RLBoundI
  73. RUBound = RUBoundI
  74. CLBound = CLBoundI
  75. CUBound = CUBoundI
  76. rows = RUBound - RLBound + 1
  77. cols = CUBound - CLBound + 1
  78. arr = newarray
  79. UBarray = UBound(arr)
  80. End Sub
  81. Sub setvartype(var)
  82. Dim tmp, i As Long
  83.  
  84. If UBarray <> -1 Then
  85. tmp = arr
  86. End If
  87.  
  88. Select Case var 'select case required for all vartypes you will use
  89. Case vbInteger
  90. Dim ii() As Integer
  91. arr = ii
  92. Case vbLong
  93. Dim ll() As Long
  94. arr = ll
  95. Case vbString
  96. Dim ss() As String
  97. arr = ss
  98. Case vbDate
  99. Dim dd() As Date
  100. arr = dd
  101. End Select
  102.  
  103. 'changes type of array coercion between array types must be possible
  104. 'and everything in range
  105. 'no error handling for this so be careful
  106. If UBarray <> -1 Then
  107. ReDim arr(UBarray)
  108. For i = 0 To UBarray
  109. arr(i) = tmp(i)
  110. Next
  111. End If
  112. vartypeset = True
  113. End Sub
  114. Sub ZeroBaseDimension(ByVal newrows As Long, ByVal newcols As Long)
  115. If newrows <= 0 Or newcols <= 0 Then Exit Sub
  116. rows = newrows
  117. cols = newcols
  118. UBarray = ((rows) * (cols)) - 1
  119. ReDim arr(UBarray)
  120. RLBound = 0
  121. CLBound = 0
  122. RUBound = rows - 1
  123. CUBound = cols - 1
  124. End Sub
  125.  
  126. Sub dimension(ByVal LBRows As Long, ByVal UBRows As Long, ByVal LBcols As Long, ByVal UBCols As Long)
  127. If UBRows < LBRows Or UBCols < LBcols Then Exit Sub
  128.  
  129. rows = UBRows - LBRows + 1
  130. cols = UBCols - LBcols + 1
  131. UBarray = ((rows) * (cols)) - 1
  132. ReDim arr(UBarray)
  133. RLBound = LBRows
  134. CLBound = LBcols
  135. RUBound = UBRows
  136. CUBound = UBCols
  137. End Sub
  138.  
  139. Sub resetRowBounds(ByVal RowLbound As Long)
  140. RUBound = RowLbound + rows - 1
  141. RLBound = RowLbound
  142. End Sub
  143. Sub resetColBounds(ByVal ColLbound As Long)
  144.  
  145. CUBound = ColLbound + cols - 1
  146. CLBound = ColLbound
  147.  
  148. End Sub
  149.  
  150. Sub redimPreserveCols(ByVal newcols As Long)
  151.  
  152. Dim tmpclbound As Long, tmprlbound As Long, oldcols As Long, rowvals As Long, i As Long, postarget As Long, possource As Long
  153. Dim tmp As Variant
  154.  
  155. If newcols <= 0 Then Exit Sub
  156. tmpclbound = CLBound
  157. tmprlbound = RLBound
  158.  
  159. tmp = arr
  160.  
  161. Dim j As Long
  162. oldcols = cols
  163.  
  164. If newcols < oldcols Then rowvals = newcols Else rowvals = oldcols
  165.  
  166. ZeroBaseDimension rows, newcols
  167.  
  168. For i = 0 To rows - 1
  169.  
  170. For j = 0 To rowvals - 1
  171. arr(postarget + j) = tmp(possource + j)
  172. Next
  173.  
  174. postarget = postarget + newcols
  175. possource = possource + oldcols
  176. Next
  177. cols = newcols
  178. CLBound = tmpclbound
  179. CUBound = CLBound + cols - 1
  180. RLBound = tmprlbound
  181. RUBound = RLBound + rows - 1
  182.  
  183.  
  184. End Sub
  185.  
  186. Sub redimPreserveROWS(ByVal newrows As Long)
  187. If newrows <= 0 Then Exit Sub
  188. UBarray = ((newrows) * (cols)) - 1
  189. ReDim Preserve arr(UBarray)
  190. rows = newrows
  191. RUBound = RLBound + rows - 1
  192. End Sub
  193. Sub redimPreserve(ByVal newrows As Long, ByVal newcols As Long)
  194. If newrows <= 0 Or newcols <= 0 Then Exit Sub
  195. redimPreserveCols newcols
  196. redimPreserveROWS newrows
  197. End Sub
  198. '*** redim by bounds
  199. Sub redimPreserveByBounds(ByVal NewRUBound As Long, ByVal NewCUBound As Long)
  200. newcols = NewRUBound - RLBound + 1
  201. newrows = NewCUBound - CLBound + 1
  202. redimPreserveCols newcols
  203. redimPreserveROWS newrows
  204. End Sub
  205.  
  206. Sub redimPreserveByLowerRowBound(ByVal NewRLBound As Long)
  207.  
  208. Dim ymove As Long, i As Long, j As Long, tmp, end1 As Long, end2 As Long
  209.  
  210. If NewRLBound > RUBound Then Exit Sub
  211.  
  212. ymove = NewRLBound - RLBound
  213.  
  214. If ymove = 0 Then Exit Sub
  215.  
  216. If ymove > 0 Then
  217. end1 = RLBound + ymove
  218. end2 = RUBound + ymove
  219. For j = RLBound To RUBound Step 1
  220. For i = CLBound To CUBound
  221. tmp = getValue(j, i)
  222. setValue j - ymove, i, tmp
  223. If j < end1 Or j > end2 Then setValue j, i, vbEmpty
  224. Next
  225. Next
  226.  
  227. End If
  228.  
  229. redimPreserveROWS RUBound - NewRLBound + 1
  230.  
  231. If ymove < 0 Then
  232. end1 = RLBound - ymove
  233. end2 = RUBound - ymove
  234. For j = RUBound To RLBound Step -1
  235. For i = CLBound To CUBound
  236. tmp = getValue(j, i)
  237. setValue j - ymove, i, tmp
  238. If j < end1 Or j > end2 Then setValue j, i, vbEmpty
  239. Next
  240. Next
  241.  
  242. End If
  243. resetRowBounds NewRLBound
  244. End Sub
  245. Sub redimPreserveByLowerColBound(ByVal NewcLBound As Long)
  246.  
  247. Dim Xmove As Long, i As Long, j As Long, tmp, end1 As Long, end2 As Long
  248.  
  249. If NewcLBound > CUBound Then Exit Sub
  250.  
  251. Xmove = NewcLBound - CLBound
  252.  
  253. If Xmove = 0 Then Exit Sub
  254.  
  255. If Xmove > 0 Then
  256. end1 = CLBound + Xmove
  257. end2 = CUBound + Xmove
  258. For j = CLBound To CUBound Step 1
  259. For i = RLBound To RUBound
  260. tmp = getValue(i, j)
  261. setValue i, j - Xmove, tmp
  262. If j < end1 Or j > end2 Then setValue i, j, vbEmpty
  263. Next
  264. Next
  265. End If
  266.  
  267. redimPreserveCols CUBound - NewcLBound + 1
  268.  
  269. If Xmove < 0 Then
  270. end1 = CLBound - Xmove
  271. end2 = CUBound - Xmove
  272. For j = CUBound To CLBound Step -1
  273. For i = RLBound To RUBound
  274. tmp = getValue(i, j)
  275. setValue i, j - Xmove, tmp
  276. If j < end1 Or j > end2 Then setValue i, j, vbEmpty
  277. Next
  278. Next
  279. End If
  280.  
  281. resetColBounds NewcLBound
  282. End Sub
  283.  
  284.  
  285. Sub redimPreserveByUpperRowBound(ByVal NewRUBound As Long)
  286. redimPreserveROWS NewRUBound - RLBound + 1
  287. End Sub
  288.  
  289. Sub redimPreserveByUpperColBound(ByVal NewCUBound As Long)
  290. redimPreserveCols NewCUBound - CLBound + 1
  291. End Sub
  292. '*** end redim by bounds
  293. Sub setValue(ByVal row As Long, ByVal col As Long, VALUE As Variant)
  294. If checkBounds(row, col) = False Then Exit Sub
  295. arr(((row - RLBound) * cols) + col - CLBound) = VALUE
  296. End Sub
  297. Function getValue(ByVal row As Long, ByVal col As Long) As Variant
  298. If checkBounds(row, col) = False Then Exit Function
  299. getValue = arr(((row - RLBound) * cols) + col - CLBound)
  300. End Function
  301.  
  302. Sub setRow(ByVal rowNo As Long, VALUE As Variant)
  303. If checkBounds(rowNo, CLBound) = False Then Exit Sub
  304. Dim i As Long
  305. For i = CLBound To CUBound
  306. setValue rowNo, i, VALUE
  307. Next
  308. End Sub
  309. Sub setcol(ByVal ColNo As Long, VALUE As Variant)
  310. If checkBounds(RLBound, ColNo) = False Then Exit Sub
  311. Dim i As Long
  312. For i = RLBound To RUBound
  313. setValue i, ColNo, VALUE
  314. Next
  315.  
  316. End Sub
  317. Sub setRange(ByVal row1 As Long, ByVal col1 As Long, ByVal row2 As Long, ByVal col2 As Long, VALUE As Variant)
  318. If checkBounds(row1, col1) = False 
  319. Ne